package com.haiqiu.system.controller;

import io.swagger.annotations.ApiParam;
import com.haiqiu.common.controller.BaseController;
import com.haiqiu.common.page.PageRequest;
import com.haiqiu.common.result.Constants;
import com.haiqiu.common.result.ResultData;
import com.haiqiu.system.entity.SysRole;
import com.haiqiu.system.entity.dto.SysRoleDto;
import com.haiqiu.system.entity.query.UserQuery;
import com.haiqiu.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author HaiQiu
 * @date 2021/4/2 23:10
 * @desc
 **/
@Api(tags = "角色管理",description = "角色删除查询等方法")
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {


    @Autowired
    private SysRoleService sysRoleService;


    @ApiOperation(value = "角色新增")
    @RequestMapping(method = RequestMethod.POST,value = "")
    public ResultData save(@RequestBody @Validated SysRoleDto roleDto){
        sysRoleService.save(roleDto);
        return decide(Constants.SUCCESS_ADD);
    }

    @ApiOperation(value = "角色批量删除")
    @RequestMapping(method = RequestMethod.DELETE,value = "")
    public ResultData delete(@ApiParam("角色ID，数组[]")@RequestBody Set<Long> ids){
        return decide(Constants.DELETE, sysRoleService.delete(ids));
    }

    @ApiOperation(value = "角色修改")
    @RequestMapping(method = RequestMethod.PUT,value = "")
    public ResultData update(@RequestBody @Validated SysRoleDto roleDto){
        sysRoleService.update(roleDto);
        return decide(Constants.SUCCESS_UPDATE);
    }

    @ApiOperation(value = "角色批量激活禁用")
    @RequestMapping(method = RequestMethod.PUT,value = "/active")
    public ResultData active(@ApiParam(value = "只需要提供角色id和激活参数即可") @RequestBody List<SysRoleDto> roleDtos){
        sysRoleService.active(roleDtos);
        return decide(Constants.SUCCESS_UPDATE);
    }

    @ApiOperation(value = "角色根据ID查询")
    @RequestMapping(method = RequestMethod.GET,value = "")
    public ResultData get(@RequestParam Long id){
        return decide(sysRoleService.get(id));
    }

    @ApiOperation(value = "角色模糊分页")
    @RequestMapping(method = RequestMethod.POST,value = "/list")
    public ResultData list(@RequestBody PageRequest<SysRole> request){
        return decide(sysRoleService.list(request));
    }

    @ApiOperation(value = "根据角色ID查询角色已授权用户列表")
    @RequestMapping(method = RequestMethod.GET,value = "/allocatedList")
    public ResultData allocatedList(@RequestParam Long roleId,
                           @RequestParam Long pageIndex,
                           @RequestParam Long pageSize,
                           @RequestParam(required = false) String username,
                           @RequestParam(required = false) String phone,
                           @RequestParam(required = false) String email){
        return decide(sysRoleService.allocatedList(roleId,pageIndex,pageSize,username,phone,email));
    }

    @ApiOperation(value = "根据角色ID查询角色未授权用户列表")
    @RequestMapping(method = RequestMethod.GET,value = "/unallocatedList")
    public ResultData unallocatedList(@RequestParam Long roleId,
                           @RequestParam Long pageIndex,
                           @RequestParam Long pageSize,
                           @RequestParam(required = false) String username,
                           @RequestParam(required = false) String phone,
                           @RequestParam(required = false) String email){
        return decide(sysRoleService.unallocatedList(roleId,pageIndex,pageSize,username,phone,email));
    }

    @ApiOperation(value = "角色查询所有")
    @RequestMapping(method = RequestMethod.POST,value = "/all")
    public ResultData all(){
        return decide(sysRoleService.all());
    }

    @ApiOperation(value = "批量取消或者批量添加用户授权角色")
    @RequestMapping(method = RequestMethod.PUT,value = "/authorize")
    public ResultData authorize(@RequestBody UserQuery userQuery){
        return decide(String.format(Constants.SUCCESS_BATCH, sysRoleService.authorize(userQuery)));
    }
}
